package com.cy.uv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.uv.enetity.Product;
import com.cy.uv.service.Productservice;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import static org.springframework.http.ResponseEntity.ok;

/**
 * 商品管理控制器
 *
 * @Author UV
 * @Date 2024/6/17 13:11
 */
@Tag(name = "ProductController", description = "商品管理接口")
@RestController
@RequestMapping("/api/products") // 统一前缀，便于管理和识别API路径
public class ProductController {

    @Autowired
    private Productservice productService;

    @Operation(summary = "获取所有商品列表", description = "返回全部商品信息，注意分页和加密处理")
    @ApiResponse(responseCode = "200", description = "成功返回商品列表", content = @Content(schema = @Schema(implementation = Product.class)))
    @GetMapping("/gPro")
    public List<Product> getProducts() {
        return productService.getProducts();
    }

    @Operation(summary = "根据ID获取商品详情", description = "通过商品ID获取商品详细信息")
    @Parameter(name = "id", description = "商品ID", required = true)
    @ApiResponse(responseCode = "200", description = "成功返回商品详情", content = @Content(schema = @Schema(implementation = Product.class)))
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Integer id) {
        return productService.getById(id);
    }

    @Operation(summary = "根据名称获取商品列表", description = "根据商品名称查询商品列表")
    @Parameter(name = "name", description = "商品名称", required = true)
    @ApiResponse(responseCode = "200", description = "成功返回商品列表", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Product.class))))
    @GetMapping("/name/{name}")
    public List<Product> getProductByName(@PathVariable String name) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("t_name", name);
        return productService.list(wrapper);
    }

    @Operation(summary = "根据分类获取商品列表", description = "根据商品分类获取商品列表")
    @Parameter(name = "sort", description = "商品分类", required = true)
    @ApiResponse(responseCode = "200", description = "成功返回商品列表", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Product.class))))
    @GetMapping("/sort/{sort}")
    public List<Product> getProductBySort(@PathVariable String sort) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("sort", sort);
        return productService.list(wrapper);
    }

    @Operation(summary = "添加商品", description = "添加一个新的商品到数据库")
    @ApiResponse(responseCode = "200", description = "操作成功,前端接收到true，alert：操作成功")
    @PostMapping("/add")
    public boolean addProduct(@RequestBody Product product) {
        try{
        productService.save(product);}
        catch (Exception e){
            ResponseEntity.badRequest().build();
        }
        return  true;
    }

/*    @Operation(summary = "删除商品", description = "根据商品ID删除商品，建议改为逻辑删除")
    @Parameter(name = "id", description = "商品ID", required = true)
    @ApiResponse(responseCode = "200", description = "操作成功")
    @DeleteMapping("/{id}")
    public boolean deleteProduct(@PathVariable Integer id) {
        return productService.removeById(id);
    }*/

    @Operation(summary = "更新商品信息", description = "根据传入的商品信息更新数据库中的记录")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @PutMapping("/update")
    public ResponseEntity<?> updateProduct(@RequestBody Product product) {
        productService.updateById(product);
        return ResponseEntity.ok().body("更新成功");
    }

    @Operation(summary = "主页显示框，固定调用", description = "分页调用显示产品在主页")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @GetMapping("/index")
    public ResponseEntity<List<Product>> getProductPage(@RequestParam(value = "pageNow", defaultValue = "1") int pagenow,
                                                        @RequestParam(value = "pageSize", defaultValue = "8") int pagesize) {
        //理论上这里要写trycatch，但是过了测试应该不需要吧········
        List<Product> productList = productService.getProductListPage(pagenow, pagesize);
        return ResponseEntity.ok(productList);
    }
}
